﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HRMS.Models;
using DataManager;
using HRMS_Model;
using Lib.Web.Mvc.JQuery.JqGrid;
using System.Globalization;
using HRMS.Models.ModelBinders;

namespace HRMS.Controllers
{
    public class ExaminationController : Controller
    {
     //   readonly Examination _model = new Examination
     //   {
     //       ExmSubSkills = Subskills.GetSubSkills().ToList()
     //   };
        //
        // GET: /Examination/

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Examinations(JqGridRequest request)
        {
            //get the count of employees
            var totalRecords = ExaminationsModels.GetExaminationsCount();

            //Prepare JqGridData instance
            var response = new JqGridResponse
            {
                //Total pages count
                TotalPagesCount = (int)Math.Ceiling((float)totalRecords / (float)request.RecordsCount),
                //Page number
                PageIndex = request.PageIndex,
                //Total records count
                TotalRecordsCount = totalRecords
            };

            //Get employees
            var examinations =ExaminationsModels.GetExaminations(request.SortingName.ToString(CultureInfo.InvariantCulture), request.SortingOrder.ToString(), request.PageIndex, request.RecordsCount, request.PagesCount.HasValue ? request.PagesCount.Value : 1);

            //Table with rows data
            foreach (var examination in examinations)
            {
                response.Records.Add(new JqGridRecord(Convert.ToString(examination.QuestionId),
                    new List<object> { 
                                    examination.QuestionId,
                examination.Question,
                 examination.OptionA,
                 examination.OptionB,
                 examination.OptionC,
                 examination.Answer,
                 examination.Isactive,
                examination.SkillId
                    }
                ));
            }

            //Return data as json
            return new JqGridJsonResult() { Data = response };
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertExamination(Examination exmModel)
        {
            return Json(ExaminationsModels.AddExamination(exmModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateExamination([ModelBinder(typeof(CustomModelBinder))]Examination exmModel)
        {
            return Json(ExaminationsModels.UpdateExamination(exmModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteExamiantion(int id)
        {
            return Json(ExaminationsModels.DeleteExamination(id));
        }

        
    }
}

        